//
//  SY_WebViewController.m
//  ChatDemo-UI2.0
//
//  Created by 薛子 on 2018/5/2.
//  Copyright © 2018年 薛子. All rights reserved.
//

#import "SY_WebViewController.h"
#import "NJKWebViewProgressView.h"
#import "NJKWebViewProgress.h"
@interface SY_WebViewController () <NJKWebViewProgressDelegate,UIWebViewDelegate>
{
    NJKWebViewProgressView *_webViewProgressView;
    NJKWebViewProgress *_webViewProgress;
    
    UIBarButtonItem *backItem;
    UILabel *titlelabel;
    UIButton *closeBtn;
    
}

@property (nonatomic, strong)UIWebView * webView;

@end



@implementation SY_WebViewController


- (UIWebView *)webView{
    
    if(_webView == nil){
        
        _webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, SafeAreaTopHeight, SCREENWIDTH, SCREENHEIGH)];
        
        NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL URLWithString:_urlStr]];
        
        [_webView loadRequest:request];
        
    }
    
    return _webView;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
   self.navBar.nav_titleLabel.text = @"校园风采";
    
    [self.view addSubview:self.webView];
    
    
    _webViewProgress = [[NJKWebViewProgress alloc] init];
    _webView.delegate = _webViewProgress;
    _webViewProgress.webViewProxyDelegate = self;
    _webViewProgress.progressDelegate = self;
    
    
    CGRect navBounds = self.navigationController.navigationBar.bounds;
    CGRect barFrame = CGRectMake(0,
                                 navBounds.size.height - 2,
                                 navBounds.size.width,
                                 2);
    _webViewProgressView = [[NJKWebViewProgressView alloc] initWithFrame:barFrame];
    _webViewProgressView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleTopMargin;
    [_webViewProgressView setProgress:0 animated:YES];
    
    _webViewProgressView.tintColor = [UIColor colorWithHexString:@"#1ebfff"];
    _webViewProgressView.progressBarView.tintColor = [UIColor colorWithHexString:@"#1ebfff"];
    //    _webViewProgressView.backgroundColor = [UIColor colorWithHexString:@"#1ebfff"];
    [self.navigationController.navigationBar addSubview:_webViewProgressView];
    
    backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageWithOriginalName:@"back2"] style:UIBarButtonItemStyleDone target:self action:@selector(back:)];
    
    [self.navigationItem setLeftBarButtonItem:backItem];
    
    UIImageView *navBackView=[[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 160, 44)];
    navBackView.userInteractionEnabled=YES;
    
    titlelabel=[[UILabel alloc]initWithFrame:CGRectMake(0, 0,160, 44)];
    titlelabel.textAlignment=NSTextAlignmentCenter;
    titlelabel.font = [UIFont boldSystemFontOfSize:18];
    titlelabel.textColor = RGBACOLOR(0, 0, 0, 1);
    titlelabel.text = @"校园风采";
    [navBackView addSubview:titlelabel];
    
    self.navigationItem.titleView=navBackView;
    
    closeBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [closeBtn setFrame:CGRectMake(65, 1, 50, 40)];
    [closeBtn setTitle:@"关闭" forState:(UIControlStateNormal)];
    closeBtn.titleLabel.font = [UIFont boldSystemFontOfSize:16];
    [closeBtn setTitleColor:[UIColor blackColor] forState:(UIControlStateNormal)];
    [closeBtn addTarget:self action:@selector(closeAction) forControlEvents:(UIControlEventTouchUpInside)];
    //    closeBtn.hidden = YES;
    [self.navigationController.navigationBar addSubview:closeBtn];
    
    
    // Do any additional setup after loading the view.
}
#pragma mark -webviewDelegate

-(void)webViewProgress:(NJKWebViewProgress *)webViewProgress updateProgress:(float)progress
{
    [_webViewProgressView setProgress:progress animated:YES];
//    self.title = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
}

#pragma mark -返回上一层控制器
-(void)back:(id)sender{
    
    
    if ([self.webView canGoBack]) {
        
        [self.webView goBack];
        
    }else{
        
        [closeBtn removeFromSuperview];
        [_webViewProgressView removeFromSuperview];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)closeAction {
    
    [_webViewProgressView removeFromSuperview];
    [closeBtn removeFromSuperview];
    [self.navigationController popViewControllerAnimated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
